﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WuxiWTO.Mapping;
using WutiWTO.Common;
using System.Data.Linq;


namespace WuxiWTO.Web.backend
{
    public partial class addNewCompany : BasePage
    {
        int cid = 0;
        /// <summary>
        /// 更新模式
        /// </summary>
        protected bool UpdateMode
        {
            get
            {
                if (!string.IsNullOrEmpty(Request.QueryString["id"]))
                {
                    cid = int.Parse(Request.QueryString["id"]);
                    ViewState["cid"] = int.Parse(Request.QueryString["id"]);
                    return true;
                }
                else
                {
                    return false;
                }
            }
        }

        WTODataContext ctx = new WTODataContext();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (UpdateMode)
                {
                    InitPageContent(cid);
                }
            }
        }

        private void InitPageContent(int cid)
        {
           top_company com = ctx.top_companies.SingleOrDefault(p => p.ID == cid);
           if (com != null)
           {
               this.txtCompany.Text = com.company_name;
               this.txtCountry.Text = com.country;
           }
        }

        protected void btnOK_Click(object sender, EventArgs e)
        {
            top_company com;
            if (!UpdateMode)
            {
                com = new top_company();
            }
            else
            {
                com = ctx.top_companies.SingleOrDefault(p => p.ID == cid);
            }
            com.company_name = this.txtCompany.Text.Trim();
            com.country = this.txtCountry.Text.Trim();
            if (!UpdateMode)
            {
                ctx.top_companies.InsertOnSubmit(com);
            }
            ctx.SubmitChanges();
            Response.Redirect("topCompany.aspx");
        }
    }
}